import { RouterItem } from "@/routers/interface/index";

/**
 * @description 递归查询对应的路由
 * @param {String} path 当前访问地址
 * @param {Array} routes 路由列表
 * @return array
 */
export const searchRoute = (path:string, routes: any[] = []): any => {
    let result = {}
    for(let item of routes) {
        if(item.path === path) {
            return item
        }
        if(item.children) {
            const res = searchRoute(path, item.children)
            if (Object.keys(res).length) {
                result = res
            }
        }
    }
    return result
}

/**
 * @description 获取需要展开的subMenu
 * @param {String} path 当前访问地址
 * @return array
 */
export const getOpenKeys = (path:string) => {
    let newStr: string = ""
    let newArr: any[] = []
    let arr = path.split("/").map(i => "/"+i)
    return arr
}

/**
 * @description 通过路由表生成侧边栏数据
 * @param {RouterItem[]} 原始路由表
 * @return {Menu.MenuOptions[]}
 */
export const generateMenuData = (routerList: RouterItem[]):any[] => {
    const menuList:any[] = []
    routerList.forEach(item => {
        // 如果没有子菜单，直接显示该菜单
        if(!item.children || item.children.length == 0 ) {
            menuList.push({
                path: item?.path,
                title: item?.meta?.title,
                icon: item?.meta?.icon,
                isLink: item?.meta?.isLink
            })
        }else{
            if(item?.children?.length == 1 && !item?.meta?.title){
                menuList.push(...generateMenuData(item.children))
            }else{
                menuList.push({
                    path: item?.path,
                    title: item?.meta?.title,
                    icon: item?.meta?.icon,
                    isLink: item?.meta?.isLink,
                    children: generateMenuData(item.children)
                })
            }
        }
    })
    return menuList
}

/**
 * @description 递归当前路由的所有关联路由，生成面包屑导航
 * @param {String} path 当前访问地址
 * @param {Array} menuList 菜单列表
 * @return array
 */
export const getBreadcrumbList = (path: string, menuList: Menu.MenuOptions[]) => {
    let tempPath: Menu.MenuOptions[] = []
    try {
		const getNodePath = (node: Menu.MenuOptions) => {
			tempPath.push(node);
			// 找到符合条件的节点，通过throw终止掉递归
			if (node.path === path) {
				throw new Error("GOT IT!");
			}
			if (node.children && node.children.length > 0) {
				for (let i = 0; i < node.children.length; i++) {
					getNodePath(node.children[i]);
				}
				// 当前节点的子节点遍历完依旧没找到，则删除路径中的该节点
				tempPath.pop();
			} else {
				// 找到叶子节点时，删除路径当中的该叶子节点
				tempPath.pop();
			}
		};
		for (let i = 0; i < menuList.length; i++) {
			getNodePath(menuList[i]);
		}
	} catch (e) {
		return tempPath.map(item => item.title);
	}
}

/**
 * @description 双重递归 找出所有 面包屑 生成对象存到 redux 中，就不用每次都去递归查找了
 * @param {String} menuList 当前菜单列表
 * @return object
 */
export const findAllBreadcrumb = (menuList:Menu.MenuOptions[]) : {[key: string]:string[]|undefined} => {
    let handleBreadcrumbList: {[key: string]:string[]|undefined} = {}
    const loop = (menuItem: Menu.MenuOptions) => {
        if(menuItem?.children?.length) {
            menuItem.children.forEach(item => loop(item))
        }else{
            handleBreadcrumbList[menuItem.path] = getBreadcrumbList(menuItem.path, menuList)
        }
    }
    menuList.forEach(item => loop(item))
    console.log(handleBreadcrumbList)
    return handleBreadcrumbList
}

/**
 * @description 获取浏览器默认语言
 * @return string
 */
export const getBrowserLang = () => {
    let browserLang = navigator.language
    let defaultBrowserLang = ""
    if(browserLang.toLowerCase() === "cn" || browserLang.toLowerCase() === "zh" || browserLang.toLowerCase() === "zh-cn") {
        defaultBrowserLang = "zh"
    } else {
        defaultBrowserLang = "en"
    }
    return defaultBrowserLang
}